﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataTransferObject
{
    /*
     * Chứa danh sách task và các methods hỗ trợ cho việc quản lý danh sách này
     */
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TaskList
    {

        #region Attributes


        /// <summary>
        /// Danh sách các task của chương trình
        /// </summary>
        private List<Task> _taskList;
        private int _lastID;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="TaskList"/> class.
        /// </summary>
        public TaskList()
        {
            _taskList = new List<Task>();
            LastID = 0;
        }

        #endregion

        #region Essential Methods

        /// <summary>
        /// Adds the task to list.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <returns></returns>
        public void AddTaskToList(Task task)
        {
            Random r = new Random();
            task.TaskCode = DateTime.Now.GetHashCode() + 1;
            Boolean isTaskValid = true;
            do
            {
                isTaskValid = true;
                for (int i = 0; i < _taskList.Count; i++)
                {
                    if (task.TaskCode.Equals(_taskList[i].TaskCode))
                    {
                        task.TaskCode += r.Next(1, 30000);
                        isTaskValid = false;
                    }
                }
            } while (!isTaskValid);
            if (task.ProjectID < 0)
                task.ProjectID = 0;
            if (task.WorkingHourID < 0)
                task.WorkingHourID = 0;
            if (task.TaskStatusIndex < 0)
                task.TaskStatusIndex = 0;
            if (task.Priority < 0)
                task.Priority = 0;
            this._taskList.Add(task);
            task.TaskID = LastID++;
            task.TaskList = this;
        }

        /// <summary>
        /// Sorts the tasklist based on priority.
        /// </summary>
        /// <returns></returns>
        public int SortPriority()
        {
            _taskList.Sort(delegate(Task task1, Task task2)
            {
                return task1.Priority.CompareTo(task2.Priority);
            });

            return _taskList.Count;
        }

        /// <summary>
        /// Sorts the tasklist based on deadline.
        /// </summary>
        /// <returns></returns>
        public int SortDeadline()
        {
            _taskList.Sort(delegate(Task task1, Task task2)
            {
                return task1.ScheduledEndDate.CompareTo(task2.ScheduledEndDate);
            });

            return _taskList.Count;
        }

        /// <summary>
        /// Gets the task at.
        /// </summary>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public Task GetTaskAt(int index)
        {
            return (Task)_taskList[index];
        }

        public Task GetTaskByID(int taskID)
        {
            Task result = null;
            for (int i = 0; i < _taskList.Count; i++)
            {
                if (_taskList[i].TaskID == taskID)
                {
                    result = _taskList[i];
                }
            }
            return result;
        }
        public TaskList Clone()
        {
            TaskList tl = new TaskList();
            foreach (Task t in _taskList)
            {
                tl.AddTaskToList(t);
            }
            return tl;
        }

        public void MapToStandardTime(DateTime startSchedule)
        {
            foreach (Task t in _taskList)
            {
                t.UpdateMappedTime(startSchedule);
            }
        }
        #endregion

        #region Properties

        public List<Task> GetTaskList
        {
            get { return _taskList; }
            set
            {
                _taskList = value;
            }
        }

        public int LastID
        {
            get { return _lastID; }
            set { _lastID = value; }
        }

        public int CurrentIndex
        {
            get { return _taskList.Count - 1; }
        }

        public int NumberOfTask
        {
            get { return this.GetTaskList.Count; }
        }

        #endregion

    }
}
